/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import interfaces.InternationalizableEntity;
import interfaces.InternationalizableEntityDescription;
import java.io.Serializable;
import java.util.List;
import java.util.Locale;
import javax.faces.context.FacesContext;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author damfercer
 */
@Entity
@Table(name = "sex")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Sex.findAll", query = "SELECT s FROM Sex s"),
    @NamedQuery(name = "Sex.findByIdSex", query = "SELECT s FROM Sex s WHERE s.idSex = :idSex"),
    @NamedQuery(name = "Sex.findByDescription", query = "SELECT s FROM Sex s WHERE s.description = :description")})
public class Sex implements Serializable,InternationalizableEntity {
    @OneToMany(mappedBy = "sex", fetch = FetchType.LAZY)
    private List<Search> searchList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idSex", fetch = FetchType.LAZY)
    private List<SexDescription> sexDescriptionList;
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID_SEX")
    private Integer idSex;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 10)
    @Column(name = "DESCRIPTION")
    private String description;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "sex", fetch = FetchType.LAZY)
    private List<Candidate> candidateList;

    public Sex() {
    }

    public Sex(Integer idSex) {
        this.idSex = idSex;
    }

    public Sex(Integer idSex, String description) {
        this.idSex = idSex;
        this.description = description;
    }

    public Integer getIdSex() {
        return idSex;
    }

    public void setIdSex(Integer idSex) {
        this.idSex = idSex;
    }

    @Override
    public String getDescription() {
//        String resDescription=description;
//        Locale currentLocale=FacesContext.getCurrentInstance().getViewRoot().getLocale();
//        if(!getSexDescriptionList().isEmpty() && !currentLocale.equals(Locale.ENGLISH)){
//            String localeCode=currentLocale.getLanguage();
//            for(SexDescription cd:getSexDescriptionList()){
//                if(cd.getIdLanguage().getCode().equals(localeCode)){
//                    resDescription=cd.getDescription();
//                }
//            }
//        }
//        return resDescription;
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @XmlTransient
    public List<Candidate> getCandidateList() {
        return candidateList;
    }

    public void setCandidateList(List<Candidate> candidateList) {
        this.candidateList = candidateList;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idSex != null ? idSex.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Sex)) {
            return false;
        }
        Sex other = (Sex) object;
        if ((this.idSex == null && other.idSex != null) || (this.idSex != null && !this.idSex.equals(other.idSex))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.Sex[ idSex=" + idSex + " ]";
    }

    @XmlTransient
    public List<SexDescription> getSexDescriptionList() {
        return sexDescriptionList;
    }

    public void setSexDescriptionList(List<SexDescription> sexDescriptionList) {
        this.sexDescriptionList = sexDescriptionList;
    }

    @XmlTransient
    public List<Search> getSearchList() {
        return searchList;
    }

    public void setSearchList(List<Search> searchList) {
        this.searchList = searchList;
    }

    @Override
    public Integer getId() {
        return getIdSex();
    }

    @Override
    public List<? extends InternationalizableEntityDescription> getDescriptionsList() {
        return getSexDescriptionList();
    }
    
}
